package com.leyou.item.controller;

import com.leyou.item.pojo.SpecGroup;
import com.leyou.item.pojo.SpecParam;
import com.leyou.item.service.SpecificationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.named.NamedContextFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.time.OffsetDateTime;
import java.util.List;

/**
 * @author 岩冰
 * @version 1.0
 * @date:2018/9/29
 */
@RestController
@RequestMapping("spec")
public class SpecificationController {
    @Autowired
    private SpecificationService specificationService;

    @GetMapping("groups/{cid}")
    public ResponseEntity<List<SpecGroup>> querySpecGroups(@PathVariable("cid") Long cid) {
        List<SpecGroup> specGroups = specificationService.querySpecGroups(cid);
        if (specGroups == null || specGroups.isEmpty()) {
            return ResponseEntity.status(HttpStatus.NO_CONTENT).body(null);
        }
        return ResponseEntity.ok(specGroups);
    }

    @PostMapping("group")
    public ResponseEntity<Void> addSpecGroup(@RequestBody SpecGroup specGroup) {
        specificationService.addSpecGroup(specGroup);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @PutMapping("group")
    public ResponseEntity<Void> updateSpecGroup( @RequestBody SpecGroup specGroup) {
        specificationService.updateSpecGroup(specGroup);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @PutMapping("group/{id}")
    public ResponseEntity<Void> updateSpecGroup(@PathVariable("id") Long id) {
        specificationService.deleteSpecGroup(id);
        return ResponseEntity.ok().build();
    }

    //下面是对param的操作
    @GetMapping("params")
    public ResponseEntity<List<SpecParam>> querySpecParams(
            @RequestParam(value="gid", required = false) Long gid,
            @RequestParam(value="cid", required = false) Long cid,
            @RequestParam(value="searching", required = false) Boolean searching,
            @RequestParam(value="generic", required = false) Boolean generic
    ) {
        List<SpecParam> specParams = specificationService.querySpecParams(gid,cid,searching,generic);
        if (specParams == null && specParams.isEmpty()) {
            return ResponseEntity.status(HttpStatus.NO_CONTENT).body(null);
        }
        return ResponseEntity.ok(specParams);
    }
    @DeleteMapping("param/{id}")
    public ResponseEntity<Void> deleteSpecParam(@PathVariable("id")Long id) {
        specificationService.deleteSpecParam(id);
        return ResponseEntity.ok().build();
    }
    @PostMapping("param")
    public ResponseEntity<Void> addSpecParam(@RequestBody SpecParam specParam) {
        specificationService.addSpecParam(specParam);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }
    @PutMapping("param")
    public ResponseEntity<Void> updateSpecParam(@RequestBody SpecParam specParam) {
        specificationService.updateSpecParam(specParam);
        return ResponseEntity.ok().build();
    }

    @GetMapping("{cid}")
   public ResponseEntity<List<SpecGroup>>querySpecsByCid(@PathVariable("cid") Long cid){
        List<SpecGroup> specGroups= this.specificationService.querySpecsByCid(cid);
        if (specGroups==null||specGroups.isEmpty()) {
            return new ResponseEntity<>(HttpStatus.NO_CONTENT);
        }
        return ResponseEntity.ok(specGroups);

    }

}
